// components/common/mobileCode/mobileCode.js
const app = getApp();
const call = require('../../../utils/request.js');
Component({
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.setData({
        phoneNumber: app.globalData.phoneNumber
      })
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  options: {
    styleIsolation: 'apply-shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    phoneNumber: '',
    timer: '',
    countDownNum: 60,
    code: '',
    forbitSubmit: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取验证码
     */
    getCode() {
      var that = this;
      let countDownNum = that.data.countDownNum;
      that.setData({
        timer: setInterval(() => {
          countDownNum--
          that.setData({
            countDownNum: countDownNum
          })
          if (countDownNum == 0) {
            clearInterval(that.data.timer);
            that.setData({
              timer: '',
              countDownNum: 60
            })
          }
        }, 1000)
      })
      const url = `/service/standard/v1/family/group/getMobileCode?access_token=${app.globalData.access_token}`;
      const params = {
        "header": app.globalData.reqHeader,
        "data": {
          "cellphone": app.globalData.phoneNumber
        }
      };
      console.log('请求', params);
      call.postData(url, params,
        res => {
          console.log('_getMobileCode_success', res);
          if (res.errormsg !== 'success') {
            wx.showToast({
              title: res.errormsg,
              icon: 'none'
            })
          }
        }, err => {
          console.log('_getMobileCode_fail', err);
          wx.showToast({
            title: '网络好像出小差了',
            icon: 'none'
          })
        }, 'api')
    },
    /**
     * 获取用户输入的验证码
     */
    subCode(e) {
      var that = this;
      this.setData({
        code: e.detail.value,
        forbitSubmit: e.detail.value != '' ? false : true
      })
    },
    /**
     * 提交验证码
     */
    onSubmit() {
      this.triggerEvent('subCode', {
        mobileCode: this.data.code
      }, {})
    },
    goBack(){
      this.triggerEvent('closeCode',{},{})
    }
  }
})